﻿using System.Collections.Generic;
using System.Linq;
using Microsoft.SharePoint;

namespace SharePoint.Orm.Helpers {
	/// <summary>
	/// Collection of helper methods for working with <see cref="SPField"/>s.
	/// </summary>
	public static class SPFieldHelper {
		public static void UpdateChoices(SPField choiceField, IEnumerable<string> choices) {
			if (choiceField is SPFieldChoice) {
				UpdateChoices((SPFieldChoice)choiceField, choices);
			} else if (choiceField is SPFieldMultiChoice) {
				UpdateChoices((SPFieldMultiChoice)choiceField, choices);
			} else {
				throw new SPOrmException("Invalid field type to update choices on.");
			}
		}

		public static void UpdateChoices(SPFieldChoice choiceField, IEnumerable<string> choices) {
			choiceField.Choices.Clear();
			choiceField.Choices.AddRange(choices.ToArray());
			choiceField.Update(true);
		}

		public static void UpdateChoices(SPFieldMultiChoice choiceField, IEnumerable<string> choices) {
			choiceField.Choices.Clear();
			choiceField.Choices.AddRange(choices.ToArray());
			choiceField.Update(true);
		}
	}
}
